package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.bean.receive.ConfirmBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageReceiveConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.library.smoothCheckBox.SmoothCheckBox;
import com.zhy.android.percent.support.PercentRelativeLayout;

public class InStorageReceiveConfirmActivity extends BaseActivity {

    private static final String SUCCESS = "1";//成功

    private static final String FOCUS = "1";//强制使用系统提供的日期，客户端不显示凭证日期和过账日期

    private TextView title, confirm, receiveNumber, purchase, supplier, totalLine, submitLine, voucherDate, postDate, isFocusTextView;

    private PercentRelativeLayout dateLayout;

    private SmoothCheckBox isFocus;

    private PercentRelativeLayout submitOverviewLayout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_receive_confirm);
        initView(InStorageReceiveConfirmActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(InStorageReceiveConfirmActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        receiveNumber = (TextView) activity.findViewById(R.id.receiveNumber);
        purchase = (TextView) activity.findViewById(R.id.purchase);
        supplier = (TextView) activity.findViewById(R.id.supplier);
        totalLine = (TextView) activity.findViewById(R.id.totalLine);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        dateLayout = (PercentRelativeLayout) activity.findViewById(R.id.dateLayout);
        voucherDate = (TextView) activity.findViewById(R.id.voucherDate);
        postDate = (TextView) activity.findViewById(R.id.postDate);
        isFocusTextView = (TextView) activity.findViewById(R.id.isFocusTextView);
        isFocus = (SmoothCheckBox) activity.findViewById(R.id.isFocus);
        submitOverviewLayout = (PercentRelativeLayout) activity.findViewById(R.id.submitOverviewLayout);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_confirm_title));
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                confirm(activity, voucherDate.getText().toString(), postDate.getText().toString(), isFocus.isChecked());
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        isFocusTextView.setVisibility(View.GONE);
        isFocus.setVisibility(View.GONE);
        isFocus.setOnCheckedChangeListener((SmoothCheckBox checkBox, boolean isChecked) -> {
            if (isChecked) {
                dateLayout.setVisibility(View.VISIBLE);
                voucherDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                postDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                voucherDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", voucherDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            voucherDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "voucherDate");
                    }
                });

                postDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", postDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            postDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "postDate");
                    }
                });
            } else {
                dateLayout.setVisibility(View.GONE);
                voucherDate.setText("");
                postDate.setText("");
            }
        });

        getData(activity);
    }

    private void confirm(Context context, String voucherDate, String postDate, boolean isFocus) {
        InStorageReceiveConfirmRequestBean params = InStorageUtils.getInStorageReceiveConfirmRequestBean(context, voucherDate, postDate, isFocus);
        InStorageReceiveConfirmRequest.request(context, params, new GsonBeanListener<ConfirmBean>() {
            @Override
            public void onSuccess(ConfirmBean bean) {
                if (SUCCESS.equals(bean.getData().getCode())) {
                    SPUtils.removeReceiveInStorageInformation(context);
                    Intent intent = new Intent(context, InStorageResultActivity.class);
                    intent.putExtra(InStorageExtra.MESSAGE, bean.getData().getMsg());
                    intent.putExtra(InStorageExtra.CONFIRM_MESSAGE, context.getResources().getString(R.string.activity_in_storage_receive_confirm_success));
                    startActivity(intent);
                } else {
                    if (!TextUtils.isEmpty(bean.getData().getMsg())) {
                        Utils.middleToast(context, bean.getData().getMsg());
                    }
                }
            }
        });
    }

    private void getData(Context context) {
        InStorageReceiveOverviewBean.DataBean data = InStorageUtils.getInStorageReceiveOverviewBean(context).getData();
        receiveNumber.setText(data.getReceiveNumber());
        purchase.setText(data.getPurchase());
        supplier.setText(data.getSupplier());
        totalLine.setText(data.getReceiveInStorageList().size() + "");
        submitLine.setText(data.getReceiveInStorageList().size() + "");
        submitOverviewLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getOverviewDialog(context, InStorageUtils.getOverViewBeanWithReceiveInSorageList(data.getReceiveInStorageList())).show();
            }
        });
    }

}
